//
//  UIControl+Events.m
//  ITC_iOS
//
//  Created by Benz.L on 04/09/2018.
//  Copyright © 2018 Benz.L. All rights reserved.
//

#import "UIControl+Events.h"
#import <objc/runtime.h>

NSString *const kActionKey = @"kActionKey";

@implementation UIButton(Events)

- (void)addEvents:(UIControlEvents)events action:(ButtonAction)action {
    objc_setAssociatedObject(self, &kActionKey, action, OBJC_ASSOCIATION_RETAIN);
    [self addTarget:self action:@selector(action:) forControlEvents:events];
}

- (void)action:(UIButton *)button {
    ButtonAction action = objc_getAssociatedObject(self, &kActionKey);
    if (action) {
        action(self);
    }
}

@end
